// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import FSMState from "../FSM/FSMState";
import Input from "../common/Input";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Walk extends FSMState {

    anim: string = 'down_walk';
    // 初始速度
    sp: cc.Vec2 = cc.v2(0, 0);

    // 速度
    speed: number = 200;

    // 按下的按键
    key: number = null;

    start() {

    }

    // 移动方法
    move(dt) {
        let lv = this.component.getComponent(cc.RigidBody).linearVelocity;
        let anim = null;
        // 动画播放  保证左右移动的优先级 > 上下移动
        if (Input.Instance.horizontal < 0) {
            anim = 'left_walk';
        } else if(Input.Instance.horizontal > 0){
            anim = 'right_walk';
        }

        else if (Input.Instance.vertical < 0) {
            anim = 'down_walk';
        } else if(Input.Instance.vertical > 0){
            anim = 'up_walk';
        }

        // 播放行走动画
        this.playAnimate(anim);

        // 移动
        lv.x = Input.Instance.horizontal * this.speed;
        lv.y = Input.Instance.vertical * this.speed;
        // 移动人物
        this.component.getComponent(cc.RigidBody).linearVelocity = lv;
    }

    playAnimate(name) {
        if (this.anim == name) return;
        this.anim = name;
        this.component.getComponent(cc.Animation).play(this.anim);
    }

    // 进入状态
    onEnter() {
        super.onEnter();
    }

    // 状态更新中
    onUpdate(dt) {
        super.onUpdate(dt);
        this.move(dt)
    }
}
